/* eslint-disable no-param-reassign,consistent-return */
/**
 * Created by IvanCai on 2017/4/19.
 */
import axios from 'axios';
import utils from '../utils';
import baseURL from '@/configs/baseUrl'
import router from '@/router/index'

const dkAxiosFile = axios.create({
  baseURL,
  timeout: 100000, // 设置超时时间
});

// eslint-disable-next-line
let loading;
dkAxiosFile.interceptors.request.use(
  (config) => {
    const token = utils.getCookie('X-Access-Token');
    config.headers = { 'X-Access-Token': token || ''}
    loading = vm.$loading({
      lock: true,
      text: 'Loading',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)',
    });
    return config;
  },
  error => Promise.reject(error),
);


dkAxiosFile.interceptors.response.use(
  response => {
    loading.close();
    return response;
  },
  (error) => {
    loading.close();
    const { status, data } = error.response;
    switch (status) {
      case 401:
        router.push('/login');
        break;
      case 403:
        vm.$message(data.error.message);
        return error.response
      case 412:
        break;
      default:
        vm.$message(data.error.message);
        return Promise.reject(data);
    }
  },
);
export default dkAxiosFile;
